<template>
  <el-dialog  draggable overflow v-model="visible" :title="titleMap[mode]" :width="1170" destroy-on-close @closed="$emit('closed')">
    <sc-water-mark ref="wm" text="已记账" :color="'rgba(128,128,128,0.1)'">
      <el-form :model="form" :rules="rules" ref="dialogForm" label-width="70px" label-position="top" class="formhead">
        <el-row style="display: flex;align-items: center;">
          <el-col :span="18">
            <span style="font-size: 25px;">记账凭证</span>
          </el-col>
          <el-col :span="3">
            <el-form-item label="凭证编号" prop="code" class="backgroudBlue">
              <el-input v-model="form.code" readonly placeholder="自动生成" class="readonly"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="3">
            <el-form-item label="凭证日期" prop="billdate" class="backgroudGreen">
              <el-date-picker v-model="form.billdate" type="date" placeholder="选择日期" format="YYYY-MM-DD"
                value-format="YYYY-MM-DD" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <sk-tabs type="border-card">
        <el-tab-pane ref="tab" label="基础信息">
          <sc-VxeTable ref="save" :gridEvts="gridEvts" :data="form.details" :gridOps="gridOps"
            :tablecode="'finance_voucher_save'">
          </sc-VxeTable>
        </el-tab-pane>
        <el-tab-pane label="附件">
          <sk-upload v-model="form.attachments" :limit="10" draggable tip="单个文件不要超过10M,请上传图像格式文件">
          </sk-upload>

        </el-tab-pane>
      </sk-tabs>
    </sc-water-mark>
    <!-- </sc-water-mark> -->
    <template #footer>
      <el-button @click="visible = false">取 消</el-button>
      <el-button type="primary" v-if="form.status_id == 0" :loading="isSaveing" @click="submit()">保 存</el-button>
    </template>
  </el-dialog>
</template>

<script>

import XEUtils from "xe-utils";
export default {
  emits: ["success", "closed"],
  components: {},
  data() {
    return {
      mode: "add",
      gridEvts: {

      },
      titleMap: {
        add: "新增",
        edit: "编辑",
      },

      form: {
        id: "",
        billdate: XEUtils.toDateString(new Date(), "yyyy-MM-dd"),
        code: "",
        details: [],
        attachments: [],
      },
      rules: {
        billdate: { required: true, message: "请选择单据日期" },
      },
      visible: false,
      isSaveing: false,
      gridOps: {
        height: 400,
        columns: [
          {
            field: "amount",
            event: {
              keyup: ({ row }) => {
                row["price"] = row["amount"] / row["se_quantity"];

              },
            },
          },
        ],
      },
    };
  },
  mounted() {
  },
  methods: {

    //显示
    open(mode = "add") {
      this.mode = mode;

      this.visible = true;

      return this;
    },
    //表单提交方法
    submit() {

      this.$refs.dialogForm.validate(async (valid) => {
        if (valid) {
          this.isSaveing = true;

          var res = await this.$API.finance.voucher.save.post(this.form);
          this.isSaveing = false;
          if (res.code == 200) {
            this.$emit("success", this.form, this.mode);
            this.visible = false;
            this.$message.success("操作成功");
          }
        }
      });


    },
    async getItems(id) {
      var res = await this.$API.finance.voucher.info.get({
        filter: [{ id: id }],
      });
      if (res.code == 200) {
        this.form = res.data
        this.$nextTick(() => {
          if (this.form.status_id == 1) {
            this.$refs.wm.create();
          }
          else {
            this.$refs.wm.clear();
          }

        })
      }
    },

    //表单注入数据
    async setData(data) {
      await this.getItems(data.main_id);

    },
  },
};
</script>
<style></style>
